package com.apanal.qlife.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.sys.model.Role;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.ResourceService;
import com.apanal.qlife.sys.service.RoleService;

/**
 * 角色控制类
 * 
 * 
 * @author shuliangxing
 * 
 * @date 2015-7-21下午5:20:44
 */
@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService service;

	@Autowired
	private ResourceService resourceService;

	/**
	 * 分页查询
	 * 
	 * @param role
	 * @param page
	 * @param model
	 * @return
	 */
	@SystemLog(module = "角色管理", operation = "分页查询角色", type = LogType.SELECT)
	@RequiresPermissions("users:role:view")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Role role, Page page, Model model) {
		PageReturn<Role> result = service.selectRelationForPage(role, page);
		model.addAttribute(Constants.DEFAULT_RESULT_NAME, result);
		return "sys/roleList";
	}

	/**
	 * 转向搜索弹出框页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toListOpen", method = RequestMethod.GET)
	public String toListOpen() {
		return "sys/roleListOpen";
	}

	/**
	 * 分页查询,json返回
	 * 
	 * @param role
	 * @param page
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "listJson", method = RequestMethod.GET)
	public PageReturn<Role> listJson(Role role, Page page, Model model) {
		return service.selectRelationForPage(role, page);
	}

	/**
	 * 转向新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:role:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toCreate(Model model) {
		setCommonData(model);
		model.addAttribute("role", new Role());
		model.addAttribute("op", "新增角色");
		return "sys/roleEdit";
	}

	/**
	 * 新增
	 * 
	 * @param role
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "角色管理", operation = "新增角色", type = LogType.CREATE)
	@RequiresPermissions("users:role:create")
	@ResponseBody
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@CurrentUser User user, Role role) {
		role.setInsertBy(user.getUseId());
		return service.insert(role);
	}

	/**
	 * 转向更新页面
	 * 
	 * @param rolId
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:role:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toUpdate(@PathVariable("id") Long rolId, Model model) {
		setCommonData(model);
		model.addAttribute("role", service.selectRelationById(rolId));
		model.addAttribute("op", "修改角色");
		return "sys/roleEdit";
	}

	/**
	 * 更新
	 * 
	 * @param role
	 * @param redirectAttributes
	 * @return
	 */
	@SystemLog(module = "角色管理", operation = "修改角色", type = LogType.UPDATE)
	@RequiresPermissions("users:role:update")
	@ResponseBody
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	public String update(@CurrentUser User user, Role role) {
		role.setLastUpdateBy(user.getUseId());
		return service.updateByIdSelective(role);
	}

	/**
	 * 转向详情页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("users:role:view")
	@RequestMapping(value = "/{id}/detail", method = RequestMethod.GET)
	public String toDetail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("role", service.selectRelationById(id));
		model.addAttribute("op", "查看角色");
		return "sys/roleDetail";
	}

	/**
	 * 硬删除
	 * 
	 * @param user
	 * @param id
	 * @return
	 */
	@SystemLog(module = "角色管理", operation = "删除角色", type = LogType.DELETE)
	@RequiresPermissions("users:role:delete")
	@ResponseBody
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable("id") Long id) {
		return service.deleteById(id);
	}

	/**
	 * 启用/停用
	 * 
	 * @param user
	 * @param role
	 * @return
	 */
	@SystemLog(module = "角色管理", operation = "启用/停用角色", type = LogType.UPDATE)
	@RequiresPermissions("users:role:start/stop")
	@ResponseBody
	@RequestMapping(value = "/{rolId}/{normal}/updateToEnableOrDisable", method = RequestMethod.GET)
	public String updateToEnableOrDisable(@CurrentUser User user, Role role) {
		role.setLastUpdateBy(user.getUseId());
		return service.updateToEnableOrDisable(role);
	}

	/**
	 * 检查角色名是否存在,修改时排除当前角色名称
	 * 
	 * @param rolId
	 *            角色id,新增时传0
	 * @param rolName
	 *            角色名称
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{rolId}/{rolName}/checkRoleNameExist", method = RequestMethod.GET)
	public Boolean checkRoleNameExist(@PathVariable("rolId") Long rolId,
			@PathVariable("rolName") String rolName) {
		return service.checkRoleNameExist(rolId, rolName);
	}

	/**
	 * 根据id字符串查询
	 * 
	 * @param ids
	 *            逗号分隔
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{ids}/selectByIds", method = RequestMethod.GET)
	public List<Role> selectByIds(@PathVariable("ids") String ids) {
		return service.selectByIds(ids);
	}

	/**
	 * 设置公共数据
	 * 
	 * @param model
	 */
	private void setCommonData(Model model) {
		model.addAttribute("resourceList", resourceService.selectResourceAuthTree());
	}

}
